/**
 * knockout分页插件
 * @class
 * @param {Object} config - json格式配置参数
 * @param {string} [config.httpClient=''] - ajax请求使用的客户端类型，可选值为: HttpClient对象; 如果传空，则使用jquery的ajax
 * @param {string} [config.baseUrl=''] - 请求根路径，仅使用jquery的ajax时有效
 * @param {Object} [config.data=''] - 要分页的数据，通常为服务器api返回结果
 * @param {callback} [config.beforeRequest='undefined'] - ajax请求前回调函数
 * @param {callback} [config.afterRequest='undefined'] - ajax请求后回调函数(包括成功或失败后)
 * @param {callback} [config.afterSuccess='undefined'] - ajax请求成功后回调函数
 * @param {callback} [config.afterError='undefined'] - ajax请求失败后回调函数
 */
function Pager(config){
	var _httpClient = config.httpClient ? $.ajax : config.httpClient;
	
	var self = this;
	
	self.data = config.data;
	self.pages = ko.observableArray();
	self.count = ko.observable();
	self.items = ko.observableArray();
	
	self.currentPage = 1;
	self.totalPage = 0;
	
	var firstPage = '|<';
	var previousPage = '<';
	var nextPage = '>';
	var lastPage = '>|';
	
	self.refreshPage = function(thisPage){
		self.data.page = thisPage;
		
		if(config.beforeRequest){
			config.beforeRequest();
		}
		
		var supress = (typeof config.supress === 'undefined' || config.supress == null) ? false : config.supress;
		
		if(_httpClient){
			
		}
		ajaxClient({
			url: config.url,
			data: self.data,
			supress: supress,
			success: function(data, textStatus, jqXHR){
				self.totalPage = data.count;
				
				self.count(data.count);
				self.items(data.items);
				
				var range = calculatePageRange(thisPage, data.count);
				var pages = [];
				pages.push({
					no: firstPage
				});
				pages.push({
					no: previousPage
				});
				for(var i = range.startPage; i <= range.endPage; i++){
					var page = {}
					page.no = i;
					if(thisPage == page.no){
						page.active = true;
					}
					pages.push(page);
				}
				pages.push({
					no: nextPage
				});
				pages.push({
					no: lastPage
				});
				
				self.pages(pages);
				
				if(config.afterSuccess){
					config.afterSuccess(data, textStatus, jqXHR);
				}
			},
			afterRequest: function(){
				if(config.afterRequest){
					config.afterRequest();
				}
			}
		});			
	};
	
	self.refreshPage(1);
	
	self.reset = function(data){
		self.currentPage = 1;
		self.totalPage = 0;
		self.data = data;
	};
	
	self.refreshBody = function(page){
		var no = page.no;
		if(no == firstPage){
			self.currentPage = 1;
		}else if(no == previousPage){
			self.currentPage--;
			if(self.currentPage <= 0){
				self.currentPage = 1;
			}
		}else if(no == lastPage){
			self.currentPage = self.totalPage;
		}else if(no == nextPage){
			self.currentPage++;
			if(self.currentPage > self.totalPage){
				self.currentPage = self.totalPage;
			}
		}else{
			self.currentPage = no;
		}
		
		self.refreshPage(self.currentPage);
	};

	/**
	 * 点击页号后，重新计算起始和结束页号
	 * @param {Object} thisPage	点击的页号
	 * @param {Object} totalPage	总页数
	 */
	function calculatePageRange(thisPage, totalPage){
		var maxPage = 10;
		var startPage = 1;
		var endPage = maxPage;
		
		// 计算显示的起始页号
		if(thisPage > 5){
			if(thisPage + 4 >= totalPage){
				// 保持10页，不再左移
				startPage = totalPage - maxPage + 1;
			}else{
				// 左移一页
				startPage = thisPage - 4;
			}
			endPage = startPage + maxPage - 1;
		}
		
		// 计算显示的结束页号
		if(endPage > totalPage){
			endPage = totalPage;
		}
		
		var range = {};
		range.startPage = startPage;
		range.endPage = endPage;
		
		return range;
	};
};
